package com.xd.common.exception;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class R<T> implements Serializable {

	private static final long serialVersionUID = 1L;
	private int code;
	private boolean success;
	private T data;
	private String msg;



	private R(int code, T data, String msg) {
		this.code = code;
		this.data = data;
		this.msg = msg;
		this.success = ResultCode.SUCCESS.code == code;
	}

	private R(int code, String msg) {
		this.code = code;
		this.msg = msg;
		this.success = ResultCode.SUCCESS.code == code;
	}


	public static <T> R<T> data(T data) {
		return data(data, "操作成功");
	}

	public static <T> R<T> data(T data, String msg) {
		return data(200, data, msg);
	}

	public static <T> R<T> data(int code, T data, String msg) {
		return new R(code, data, data == null?"暂无承载数据":msg);
	}

	public static <T> R<T> success(String msg) {
		return new R(ResultCode.SUCCESS.getCode(), msg);
	}

	public static <T> R<T> fail(String msg) {
		return new R(ResultCode.FAILURE.getCode(), msg);
	}

	public static <T> R<T> fail(int code, String msg) {
		return new R(code, (Object)null, msg);
	}


	public static <T> R<T> status(boolean flag) {
		return flag?success("操作成功"):fail("操作失败");
	}

	public int getCode() {
		return this.code;
	}

	public boolean isSuccess() {
		return this.success;
	}

	public T getData() {
		return this.data;
	}

	public String getMsg() {
		return this.msg;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public void setData(T data) {
		this.data = data;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}


	public String toString() {
		return "R(code=" + this.getCode() + ", success=" + this.isSuccess() + ", data=" + this.getData() + ", msg=" + this.getMsg() + ")";
	}
}
